// import { checkValue } from '@/api/data'
/* _库 */
import _ from "lodash";

import { deepClone } from "@/utils/index";
// 异步验证示例
// const validateVersion = async (rule, value) => {
//
//   const res = await checkValue()
//   if (res.code) {
//
//
//     if (res.data.value === value && rule.required) {
//       return Promise.reject('所属应用已存在.')
//     } else {
//       return Promise.resolve()
//     }
//   }
// }

//loash判断[],'',null,undefined,{}
function isBlank(value) {
  return _.isNil(value) || _.isEmpty(value);
}
//自定义验证
const funA = (rule, value) => {
  if (rule.required && value?.length > 2) {
    return Promise.reject("所属应用不能超过2位数.");
  } else {
    return Promise.resolve();
  }
};
const funB = (rule, value) => {
  if (rule.required && value?.length > 3) {
    return Promise.reject("描述不能超过3位数.");
  } else {
    return Promise.resolve();
  }
};
const funC = (rule, value) => {
  if (rule.required && isBlank(value)) {
    return Promise.reject("部门必填.");
  } else {
    return Promise.resolve();
  }
};
const validatorReaders = (rule, value) => {
  if (rule.required && isBlank(value)) {
    return Promise.reject("阅读者必填.");
  } else {
    return Promise.resolve();
  }
};
// 定义装饰器函数，用于包装原始函数并添加标识符
// function withIdentifier(identifier) {
//   return function (func) {
//     func.identifier = identifier
//     return func
//   }
// }
// const AWithId = withIdentifier('funA')(funA)
// const BWithId = withIdentifier('funB')(funB)

const initRules = {
  basicInfoForm: {
    subject: [
      {
        required: true,
        message: "主题必填.",
        triggerCustom: ["submit"],
        groups: [
          ["basicInfoForm", "requesterTel"],
          ["helpInfo", "description"],
        ],
      }, //requester有一个有值，subject必须校验,两个都没填写不需要校验
    ],
    requester: [
      { required: true, triggerCustom: ["submit"], message: "申请人必填." },
    ],
    requesterDept: [
      {
        required: true,
        markFun: "funA",
        triggerCustom: ["submit"],
        validator: funC,
      },
    ],
    requesterTel: [
      { required: true, triggerCustom: ["submit"], message: "座机必填." },
    ],
    version2: [
      { required: true, triggerCustom: ["submit"], message: "所属应用必填." },
      { validator: funA, markFun: "funA" },
    ],
  },
  helpInfo: {
    description: [
      { triggerCustom: ["submit"], required: true, message: "描述必填." },
      { validator: funB, markFun: "funB" },
    ],
    readers: [
      { required: true, triggerCustom: ["save"], validator: validatorReaders },
    ],
  },
};
/* 判断groups验证规则start */
// 去掉合法的数据
function filtered(data, list) {
  const listSet = new Set(list);
  const filteredData = data.filter((item) => {
    return !(
      item.name.length === list.length &&
      item.name.every((name) => listSet.has(name)) &&
      list.every((name) => item.name.includes(name))
    );
  });
  return filteredData;
}

// 定义一个函数来处理不合法的数据
function transformData(LZJzV1) {
  for (const key in LZJzV1) {
    if (
      window["\x4f\x62\x6a\x65\x63\x74"][
        "\x70\x72\x6f\x74\x6f\x74\x79\x70\x65"
      ]["\x68\x61\x73\x4f\x77\x6e\x50\x72\x6f\x70\x65\x72\x74\x79"][
        "\x63\x61\x6c\x6c"
      ](LZJzV1, key)
    ) {
      const subObject = LZJzV1[key];
      if (
        subObject &&
        window["\x4f\x62\x6a\x65\x63\x74"][
          "\x70\x72\x6f\x74\x6f\x74\x79\x70\x65"
        ]["\x68\x61\x73\x4f\x77\x6e\x50\x72\x6f\x70\x65\x72\x74\x79"][
          "\x63\x61\x6c\x6c"
        ](subObject, "\x30")
      ) {
        LZJzV1[key] = subObject["\x30"];
      }
    }
  }
  return LZJzV1;
}
// 使用 reduce 来动态访问对象的嵌套属性
function getNestedValue(obj, keys) {
  return keys.reduce((acc, key) => acc && acc[key], obj);
}
// 处理验证规则的函数，根据groups里变量对应值判断是否必填
function requesterRules(EpQqg1, dlynSz2, ibsMD3, NqFMST4) {
  const demoRules = deepClone(EpQqg1);
  const rules = {};
  for (const formKey in demoRules) {
    rules[formKey] = {};
    for (const fieldKey in demoRules[formKey]) {
      const fieldRules = _[
        "\x69\x73\x50\x6c\x61\x69\x6e\x4f\x62\x6a\x65\x63\x74"
      ](demoRules[formKey][fieldKey])
        ? [demoRules[formKey][fieldKey]]
        : demoRules[formKey][fieldKey];
      const processedFieldRules = fieldRules["\x6d\x61\x70"]((rule, index) => {
        if (
          rule["\x74\x72\x69\x67\x67\x65\x72\x43\x75\x73\x74\x6f\x6d"] &&
          !_["\x69\x6e\x63\x6c\x75\x64\x65\x73"](
            rule["\x74\x72\x69\x67\x67\x65\x72\x43\x75\x73\x74\x6f\x6d"],
            ibsMD3
          )
        ) {
          rule["\x72\x65\x71\x75\x69\x72\x65\x64"] = false;
        }
        if (
          window["\x4f\x62\x6a\x65\x63\x74"][
            "\x70\x72\x6f\x74\x6f\x74\x79\x70\x65"
          ]["\x68\x61\x73\x4f\x77\x6e\x50\x72\x6f\x70\x65\x72\x74\x79"][
            "\x63\x61\x6c\x6c"
          ](rule, "\x76\x61\x6c\x69\x64\x61\x74\x6f\x72")
        ) {
          console["\x6c\x6f\x67"]("\x33\x33\x33\x33\x33", rule);
          fieldRules[index]["\x72\x65\x71\x75\x69\x72\x65\x64"] =
            fieldRules[0]["\x72\x65\x71\x75\x69\x72\x65\x64"];
        }
        if (
          window["\x4f\x62\x6a\x65\x63\x74"][
            "\x70\x72\x6f\x74\x6f\x74\x79\x70\x65"
          ]["\x68\x61\x73\x4f\x77\x6e\x50\x72\x6f\x70\x65\x72\x74\x79"][
            "\x63\x61\x6c\x6c"
          ](rule, "\x76\x61\x6c\x69\x64\x61\x74\x6f\x72") &&
          window["\x4f\x62\x6a\x65\x63\x74"][
            "\x70\x72\x6f\x74\x6f\x74\x79\x70\x65"
          ]["\x68\x61\x73\x4f\x77\x6e\x50\x72\x6f\x70\x65\x72\x74\x79"][
            "\x63\x61\x6c\x6c"
          ](rule, "\x6d\x61\x72\x6b\x46\x75\x6e")
        ) {
          if (rule["\x72\x65\x71\x75\x69\x72\x65\x64"]) {
            console["\x6c\x6f\x67"]("\x32\x32\x32\x32", rule);
            rule["\x72\x65\x71\x75\x69\x72\x65\x64"] = _[
              "\x69\x6e\x63\x6c\x75\x64\x65\x73"
            ](NqFMST4, rule["\x6d\x61\x72\x6b\x46\x75\x6e"]);
          }
        }
        if (
          ibsMD3 &&
          rule["\x74\x72\x69\x67\x67\x65\x72\x43\x75\x73\x74\x6f\x6d"] &&
          _["\x69\x6e\x63\x6c\x75\x64\x65\x73"](
            rule["\x74\x72\x69\x67\x67\x65\x72\x43\x75\x73\x74\x6f\x6d"],
            ibsMD3
          ) &&
          rule["\x67\x72\x6f\x75\x70\x73"]
        ) {
          const checkGroup = rule["\x67\x72\x6f\x75\x70\x73"][
            "\x73\x6f\x6d\x65"
          ](
            (group) =>
              group &&
              group["\x6c\x65\x6e\x67\x74\x68"] > 0 &&
              !_["\x69\x73\x45\x6d\x70\x74\x79"](getNestedValue(dlynSz2, group))
          );
          rule["\x72\x65\x71\x75\x69\x72\x65\x64"] = checkGroup;
        }
        return rule;
      });
      rules[formKey][fieldKey] = processedFieldRules;
    }
  }
  return transformData(rules);
}
/* 判断groups验证规则end */

//手动增加单个验证规则~其中data是key的层级，required是必填，validator自定义验证，message是提示信息
function customRules(l1, pXpD2, aC3, EjSTTNMG4) {
  let rules = deepClone(initRules);
  l1["\x72\x65\x64\x75\x63\x65"]((acc, key, index) => {
    if (index === l1["\x6c\x65\x6e\x67\x74\x68"] - 1) {
      if (!acc[key]) {
        acc[key] = [];
      }
      acc[key] = [
        { required: pXpD2, message: EjSTTNMG4 },
        ...(aC3 ? [{ validator: aC3 }] : []),
      ];
    } else if (!acc[key]) {
      acc[key] = {};
    }
    return acc[key];
  }, rules);
  return rules;
}
export { requesterRules, customRules, initRules, filtered };
